﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using SmesharikiMVC.Models.VideoCollection;

namespace SmesharikiMVC.Controllers
{
    public class HomeController : Controller
    {
        private VideoCollectionContext db = new VideoCollectionContext();

        public ActionResult Index(int? albumId)
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            if (!albumId.HasValue)
                albumId = 0;

            return View(albumId.Value);
        }

        public ActionResult About()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult Album(int pageId)
        {
            int albumStartIdx = 3 * pageId;
            int albumEndIdx = 3 * pageId + 3;

            IEnumerable<AlbumVideo> albumVideos = db.AlbumVideos
                                                    .Include(a => a.VideoAlbum)
                                                    .Include(v => v.YoutubeVideo)
                                                    .Where(e => e.VideoAlbumID > albumStartIdx
                                                             && e.VideoAlbumID <= albumEndIdx)
                                                    .OrderBy(a => a.VideoAlbumID)
                                                    .ToArray();

            if (albumVideos.Count() == 0)
                return PartialView("_NoAlbums");

            return PartialView("_Albums", albumVideos);
        }


        [ChildActionOnly]
        public ActionResult Admin()
        {
            return PartialView("_Admin");
        }

    }
}
